\documentclass{article}


% load package with some of the available options - you may not need this!
\usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}

% something NOT relevant to the usage of the package.
\usepackage{url,textcomp}
\setlength{\parindent}{0pt}
\setlength{\parskip}{18pt}
\title{\texttt{mcode.sty} Demo}
\author{Florian Knorn, \texttt{florian@knorn.org}}
% //////////////////////////////////////////////////

\begin{document}

\maketitle

\begin{center}
\begin{minipage}{.75\linewidth}
	\color{red}{\hfill\textbf{NOTE --- BEFORE YOU START}\hfill\strut}

	All that this package does is to configure the \verb|listings| package for you. If anything is not working the way you want it, refer to the \verb|listings| documentation first and / or take a look at the \verb|mcode.sty| file itself, which is well documented internally.\\
	
	The \verb|listings| documentation can be accessed either by typing \verb|texdoc listings| into a command prompt on your system, or online:\\\scriptsize\url{http://mirrors.ctan.org/macros/latex/contrib/listings/listings.pdf}
	
\end{minipage}
\end{center}

\section*{Installation of the package}

As with any other small package, just place the \verb|mcode.sty| file in the same folder as your document, or put it somewhere where \LaTeX{} can find it.  Done!

\medskip

\section*{Usage --- 3 ways}

1) This inline demo \mcode{for i=1:3, disp('cool'); end;} uses the \verb|\mcode{}| command.\footnote{Works also in footnotes: \mcodefn{for i=1:3, disp('cool'); end;}}

2) The following is a block using the \verb|lstlisting| environment.
\begin{lstlisting}
for i = 1:3
	if i >= 5 && a ~= b       % literate programming replacement
		disp('cool');           % comment with some §\mcommentfont\LaTeX in it: $\mcommentfont\pi x^2$§
	end
	[:,ind] = max(vec);
	x_last = x(1,end) - 1;
	v(end);
	really really long really really long really really long really really long really really long line % blaaaaaaaa
	ylabel('Voltage (µV)');
end
\end{lstlisting}
Note: Here, the package was loaded with the \verb|framed|, \verb|numbered|, \verb|autolinebreaks| and \verb|useliterate| options.  \textbf{Please see the top of mcode.sty for a detailed explanation of these options.}


3) Finally, you can also directly include an external m-file from somewhere on your hard drive (the very code you use in \textsc{Matlab}, if you want) using the \verb|\lstinputlisting{/SOME/PATH/FILENAME.M}| command.  If you only want to include certain lines from that file (for instance to skip a header), you can use \verb|\lstinputlisting[firstline=6, lastline=15]{/SOME/PATH/FILENAME.M}|.

\section*{FAQ}

\begin{description}
	\item[Why does {\tt delta} get replaced by $\Delta$, {\fontfamily{ptm}\selectfont\texttildelow=} by $\neq$, etc.?]
		Well, that's precisely what the \verb|useliterate| option does. If you don't want that, don't use it.
	\item[Can I get contiguous line numbers from one code block to another?]
		Yes, but you have to read the \verb|listings| documentation for that (Section~4.8 in particular).
	\item[{\tt mcode.sty} doesn't work in my document!] Well, try your (Matlab) code fragment in this demo document here to see whether there's something in it that might be causing a problem (not so likely, but possible), or if there's some conflict between the \verb|listings| package and some other package you have loaded.
	\item[Is feature XYZ possible?] Well, the \verb|listings| package might already be able to do that. Please consult its documentation (see red box at the top)!
\end{description}


\end{document}